//
//  ViewController.m
//  04-汤姆猫
//
//  Created by wangneihan on 15/12/11.
//  Copyright © 2015年 wangneihan. All rights reserved.
//

#import "ViewController.h"

#define  BTN_ACTION_CYMBAL 1
#define  BTN_ACTION_DRINK 2
#define  BTN_ACTION_EAT 3
#define  BTN_ACTION_FART 4
#define  BTN_ACTION_PIE 5
#define  BTN_ACTION_SCRATCH 6

@interface ViewController ()

@property (nonatomic,strong) IBOutlet UIButton *cymbalBtn;

@property (nonatomic,strong) IBOutlet UIButton *drinkBtn;

@property (nonatomic,strong) IBOutlet UIButton *eatBtn;

@property (nonatomic,strong) IBOutlet UIButton *fartBtn;

@property (nonatomic,strong) IBOutlet UIButton *pieBtn;

@property (nonatomic,strong) IBOutlet UIButton *scratchBtn;

@property (nonatomic,strong) IBOutlet UIImageView *backImgView;

-(IBAction)btnClivk:(UIButton *)btn;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //添加场景
    [self addBtns];
    
    // Do any additional setup after loading the view, typically from a nib.
}

-(void)addBtns
{
    CGRect rect = [[UIScreen mainScreen] bounds];
    CGSize size = rect.size;
    CGFloat width = size.width;
    CGFloat height = size.height;
    
    //创建背景图片
    self.backImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
    self.backImgView.image = [UIImage imageNamed:@"backImg"];
    self.backImgView.contentMode = UIViewContentModeScaleToFill;
    [self.view addSubview:self.backImgView];
    
    //获取文件全路径 用到再说
    //[NSBundle mainBundle] pathForResource:<#(nullable NSString *)#> ofType:<#(nullable NSString *)#>
    
    int baseHighe = height * 0.6;
    
    //左侧按钮
    self.cymbalBtn = [[UIButton alloc] initWithFrame:CGRectMake(5 , baseHighe, 60, 60)];
    [self.cymbalBtn setBackgroundImage:[UIImage imageNamed:@"cymbal"] forState:UIControlStateNormal];
    [self.cymbalBtn addTarget:self action:@selector(btnClivk:) forControlEvents:UIControlEventTouchUpInside];
    [self.cymbalBtn setTag:BTN_ACTION_CYMBAL];
    [self.view addSubview:self.cymbalBtn];
    
    self.drinkBtn = [[UIButton alloc] initWithFrame:CGRectMake(5 , baseHighe + 65, 60, 60)];
    [self.drinkBtn setBackgroundImage:[UIImage imageNamed:@"drink"] forState:UIControlStateNormal];
    [self.drinkBtn addTarget:self action:@selector(btnClivk:) forControlEvents:UIControlEventTouchUpInside];
    [self.drinkBtn setTag:BTN_ACTION_DRINK];
    [self.view addSubview:self.drinkBtn];
    
    self.eatBtn = [[UIButton alloc] initWithFrame:CGRectMake(5 , baseHighe + 65 + 65, 60, 60)];
    [self.eatBtn setBackgroundImage:[UIImage imageNamed:@"eat"] forState:UIControlStateNormal];
    [self.eatBtn addTarget:self action:@selector(btnClivk:) forControlEvents:UIControlEventTouchUpInside];
    [self.eatBtn setTag:BTN_ACTION_EAT];
    [self.view addSubview:self.eatBtn];
    
    //右侧按钮
    self.fartBtn = [[UIButton alloc] initWithFrame:CGRectMake(width - 65 , baseHighe, 60, 60)];
    [self.fartBtn setBackgroundImage:[UIImage imageNamed:@"fart"] forState:UIControlStateNormal];
    [self.fartBtn addTarget:self action:@selector(btnClivk:) forControlEvents:UIControlEventTouchUpInside];
    [self.fartBtn setTag:BTN_ACTION_FART];
    [self.view addSubview:self.fartBtn];
    
    self.pieBtn = [[UIButton alloc] initWithFrame:CGRectMake(width - 65 , baseHighe + 65, 60, 60)];
    [self.pieBtn setBackgroundImage:[UIImage imageNamed:@"pie"] forState:UIControlStateNormal];
    [self.pieBtn addTarget:self action:@selector(btnClivk:) forControlEvents:UIControlEventTouchUpInside];
    [self.pieBtn setTag:BTN_ACTION_PIE];
    [self.view addSubview:self.pieBtn];
    
    self.scratchBtn = [[UIButton alloc] initWithFrame:CGRectMake(width - 65 , baseHighe + 65 + 65, 60, 60)];
    [self.scratchBtn setBackgroundImage:[UIImage imageNamed:@"scratch"] forState:UIControlStateNormal];
    [self.scratchBtn addTarget:self action:@selector(btnClivk:) forControlEvents:UIControlEventTouchUpInside];
    [self.scratchBtn setTag:BTN_ACTION_SCRATCH];
    [self.view addSubview:self.scratchBtn];

}

-(IBAction)btnClivk:(UIButton *)btn
{
    if(!self.backImgView.isAnimating)
    {
        switch (btn.tag) {
            case BTN_ACTION_CYMBAL:
                [self playFlash:@"cymbal"];
                break;
            case BTN_ACTION_DRINK:
                [self playFlash:@"drink"];
                break;
            case BTN_ACTION_EAT:
                [self playFlash:@"eat"];
                break;
            case BTN_ACTION_FART:
                [self playFlash:@"fart"];
                break;
            case BTN_ACTION_PIE:
                [self playFlash:@"pie"];
                break;
            case BTN_ACTION_SCRATCH:
                [self playFlash:@"scratch"];
                break;
            default:
                break;
        }
    }
}

-(void)playFlash:(NSString *) fileName {
    //1. 从plist中读取数组
    NSArray *flashArry = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"flashList" ofType:@"plist"]];
    //2. 找到对应动画的字典
    for(int x=0 ; x < flashArry.count; x++)
    {
        if([flashArry[x][@"fileName"] isEqualToString:fileName])
        {
            NSDictionary *tempDic = flashArry[x];
            NSMutableArray *picArray = [[NSMutableArray alloc] init];
            for(int i =0 ;i < [NSString stringWithFormat:@"%@",tempDic[@"picCount"]].intValue; i ++ )
            {
                //imageNamed 将图片存入缓存。内存会爆掉
                //[picArray addObject:[UIImage imageNamed:[NSString stringWithFormat:@"%@_%2d",tempDic[@"fileName"],i]]];
                
                NSString *fileName = [NSString stringWithFormat:@"%@_%02d",tempDic[@"fileName"],i];
                
                // 必须copy到目录 才能被引用到
                //NSString *imgPath = [[NSBundle mainBundle]pathForResource:@"/Animations/Angry/angry_00" ofType:@"jpg"];
                NSString *imgPath = [[NSBundle mainBundle]pathForResource:fileName ofType:@"jpg"];

                [picArray addObject:[UIImage imageWithContentsOfFile:imgPath]];
                
                
            }
            self.backImgView.animationImages = picArray;
            self.backImgView.animationRepeatCount = 1 ;
            self.backImgView.animationDuration = [NSString stringWithFormat:@"%@",tempDic[@"times"]].doubleValue ;
            [self.backImgView startAnimating];
            //释放数组。内存降低
            [self.backImgView performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:self.backImgView.animationDuration + 1];
        }
    }
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
